1

In the addon of the session, we calculated the distances to the closest hospitals located within North-Rhine Westphalia (NRW). Still, we did not show how to subset the original file, which contains all hospitals in Germany.

Subset the data file yourself by relying on the spatial information of the file hospital_points.csv and a polygon of NRW only. How many hospitals are located within the borders of NRW?
You need two datasets for that: the point layer hospital_points.csv in the ./data folder and polygons of NRW. For the latter, you can again use the osmdata syntax.

The default of sf::st_join() will leave you with a ‘left-join’ and returns a data object with all hospitals and matching district information for those which are located within NRW. You can reset the option to perform an ‘inner-join’ and keep only the observation which lay within the predefined area (sf::st_join(x , y, join = "", left = FALSE)).

# load hospitals
hospitals <- 
  read.csv(
    "./data/hospital_points.csv", 
    header = TRUE, 
    fill = TRUE, 
    sep = ","
  ) %>%
  sf::st_as_sf(coords = c("X", "Y"), crs = 3035)


#  use the OSM function
nrw <-
  osmdata::getbb(
    "Nordrhein-Westfalen", 
    format_out = "sf_polygon"
  ) %>% 
  .$multipolygon %>% 
  sf::st_transform(3035)

# spatial join
nrw_hospitals <-
  hospitals %>% 
  sf::st_join(
    # point layer nrw
    nrw, 
    # chose intersect or within
    join = sf::st_intersects,
    # option FALSE will 
    # keep only the hospital 
    # which could be joined
    left = FALSE
  )

nrw_hospitals
## Simple feature collection with 344 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 4039950 ymin: 3058862 xmax: 4277224 ymax: 3246338
## Projected CRS: ETRS89-extended / LAEA Europe
## First 10 features:
##                                                                                              name
## 319                                                                            Ev. Krankenhaus   
## 320 Fliedner Klinik Ambulanz und Tagesklinik f\xfcr Psychiatrie, Psychotherapie und Psychosomatik
## 321                          Johanniter-Tagesklinik Klinik f\xfcr Psychiatrie und Psychotherapie 
## 322                                                       Krankenanstalten Florence Nightingale  
## 323                                                           Krankenhaus M\xf6rsenbroich-Rath   
## 324         LVR-Klinikum D\xfcsseldorf Klinikum der Heinrich-Heine- Universit\xe4t D\xfcsseldorf 
## 325                                                                            Marien-Hospital   
## 326                                            Medical Center D\xfcsseldorf -Luisenkrankenhaus-  
## 327                     Paracelsus Klinik Golzheim Fachklinik f\xfcr Urologie und Kinderurologie 
## 328                                                                Sana Kliniken D\xfcsseldorf   
##     year beds     ags                geometry
## 319 2017  568 5111000 POINT (4095599 3127379)
## 320 2017    . 5111000 POINT (4096285 3128719)
## 321 2017    . 5111000 POINT (4101713 3121737)
## 322 2017  568 5111000 POINT (4094348 3137304)
## 323 2017  718 5111000 POINT (4099466 3133197)
## 324 2017  495 5111000 POINT (4100738 3130402)
## 325 2017  461 5111000 POINT (4096526 3129923)
## 326 2017   42 5111000 POINT (4099035 3129691)
## 327 2017   81 5111000 POINT (4094841 3132032)
## 328 2017  490 5111000 POINT (4101452 3130494)
# 344 hospitals in NRW

2

Did the operationalization of health care provision convince you? Don’t you think it might be more important how many hospitals are close to survey respondents? To test this, we want to calculate the number of hospitals (and/or hospital beds) per district in North-Rhine Westphalia.

You need a dplyr::as_tibble() data frame to use the functions dplyr::group_by() and dplyr::summarise().
The function dplyr::n() allows summarizing the total count of hospitals. sum(beds) for summarizing the bed total per district.
nrw_districts <- 
  sf::read_sf("./data/VG250_KRS.shp") %>% 
  sf::st_transform(3035) %>% 
  sf::st_join(nrw, join = sf::st_intersects, left = FALSE)

nrw_hospitals <-  
  nrw_hospitals %>% 
  # beds were character, now numeric
  dplyr::mutate(beds = as.numeric(beds)) %>%
  # replace NAs as zeros for simplification
  replace(is.na(.), 0)

district_hospital_join <-
  nrw_hospitals %>% 
  # join the hospitals 
  # within districts
  sf::st_join(nrw_districts, join = sf::st_within) %>% 
  # use as tibble to perform
  # group by & summarise
  dplyr::as_tibble() %>% 
  dplyr::group_by(AGS) %>% 
  dplyr::summarise(
    hospital_count = dplyr::n(), 
    hospital_bed_count = sum(as.numeric(beds))
  ) %>% 
  # left join the new information
  # to the original data frame
  dplyr::left_join(nrw_districts, .)
## Joining with `by = join_by(AGS)`
district_hospital_join 
## Simple feature collection with 73 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 4031313 ymin: 2972671 xmax: 4332216 ymax: 3337853
## Projected CRS: ETRS89-extended / LAEA Europe
## # A tibble: 73 × 26
##      ADE    GF   BSG ARS   AGS   SDV_ARS      GEN   BEZ     IBZ BEM   NBD   SN_L 
##    <int> <int> <int> <chr> <chr> <chr>        <chr> <chr> <int> <chr> <chr> <chr>
##  1     4     4     1 03155 03155 031550011011 Nort… Land…    43 --    ja    03   
##  2     4     4     1 03251 03251 032510012012 Diep… Land…    43 --    ja    03   
##  3     4     4     1 03252 03252 032520006006 Hame… Land…    43 --    ja    03   
##  4     4     4     1 03255 03255 032550023023 Holz… Land…    43 --    ja    03   
##  5     4     4     1 03256 03256 032560022022 Nien… Land…    43 --    ja    03   
##  6     4     4     1 03257 03257 032570035035 Scha… Land…    43 --    ja    03   
##  7     4     4     1 03404 03404 034040000000 Osna… Krei…    40 --    ja    03   
##  8     4     4     1 03454 03454 034540035035 Emsl… Land…    43 --    ja    03   
##  9     4     4     1 03456 03456 034560015015 Graf… Land…    43 --    ja    03   
## 10     4     4     1 03459 03459 034040000000 Osna… Land…    43 --    ja    03   
## # ℹ 63 more rows
## # ℹ 14 more variables: SN_R <chr>, SN_K <chr>, SN_V1 <chr>, SN_V2 <chr>,
## #   SN_G <chr>, FK_S3 <chr>, NUTS <chr>, ARS_0 <chr>, AGS_0 <chr>, WSK <date>,
## #   DEBKG_ID <chr>, geometry <MULTIPOLYGON [m]>, hospital_count <int>,
## #   hospital_bed_count <dbl>